using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using OpenComposite.Repository.Properties;
using OpenComposite.EII.Repository;
using System.IO;
using System.Xml;

namespace OpenComposite.EII.RepositoryUIs
{
	public partial class FormCreateNewLogWMeth : Forms.FormCreateNewBase
	{
		#region Initialization
		public FormCreateNewLogWMeth(Repository.LogicalWebMethod item, bool bAllowContinue)
			: base(item, item, bAllowContinue)
		{
			InitializeComponent();

			this.SuspendLayout();
			try {
				if ( this.RepositoryItem != null ) {
					this.Text = string.Format(Resources.msgCreateNew, this.RepositoryItem.RepositoryItemTypeName);
				}
			} finally {
				this.ResumeLayout(true);
			}
		}
		#endregion

		#region Properties

		public Repository.BusinessObject InputBusinessObject
		{
			get { return _inputBO; }
			set
			{
				_inputBO = value;
				txtInput.Text = ( _inputBO == null ? "" : _inputBO.Name );
			}
		}
		public Repository.BusinessObject OutputBusinessObject
		{
			get { return _outputBO; }
			set
			{
				_outputBO = value;
				txtOutput.Text = ( _outputBO == null ? "" : _outputBO.Name );
			}
		}

		#endregion

		#region Variables

		//private Repository.Noun _item = null;
		private Repository.BusinessObject _inputBO = null;
		private Repository.BusinessObject _outputBO = null;
		#endregion

		#region Methods

		protected override void OnValidateNewItem(CancelEventArgs e)
		{
			DataTable dt = Global.Data.SearchRepositoryItems(
				Global.Status.CurrentDomainId, this.RepositoryItem.RepositoryItemType,
				txtName.Text, null, true);
			if ( dt != null && dt.Rows.Count > 0 ) {
				MessageBox.Show("An item with this name exists in the current domain." + Environment.NewLine + Environment.NewLine +
								"Please enter an other name.",
								this.Text, MessageBoxButtons.OK, MessageBoxIcon.Stop);
				txtName.Focus();
				txtName.SelectAll();
				e.Cancel = true;
			}
			base.OnValidateNewItem(e);
		}
		protected override void OnSaveClose(CancelEventArgs e)
		{
			if ( !save() ) {
				e.Cancel = true;
				return;
			}
			base.OnSaveClose(e);
		}
		protected override void OnSaveContinue(CancelEventArgs e)
		{
			if ( !save() ) {
				e.Cancel = true;
				return;
			}
			base.OnSaveContinue(e);
		}

		private bool save()
		{
			if ( txtName.Text == "" ) {
				MessageBox.Show("Please enter a name.", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
				txtName.Focus();
				return false;
			}

			Repository.LogicalWebMethod item = this.RepositoryItem as Repository.LogicalWebMethod;
			if ( item == null )
				throw new Exception("Repository Item is not a noun.");

			item.Name = txtName.Text;
			item.Description = txtDesc.Text;
			item.InputBusinessObject = _inputBO;
			item.OutputBusinessObject = _outputBO;
			item.DomainId = this.DomainID;

			return true;
		}

		private void showSchema(BusinessObject bo)
		{
			if ( bo == null || bo.Schema == null )
				return;

			XmlDocument xd = new XmlDocument();
			using ( StringWriter sw = new StringWriter() ) {
				bo.Schema.Write(sw);
				xd.LoadXml(sw.ToString());
			}
			formXmlViewer XmlViewer = new formXmlViewer();
			XmlViewer.XmlDocument = xd;
			XmlViewer.Text = "XML-Schema of Logical Contract \"" + bo.Name + "\"";
			XmlViewer.ShowDialog();
		}
		private void showInputOutput()
		{
			txtInput.Text = ( _inputBO == null ? "" : _inputBO.Name );
			txtOutput.Text = ( _outputBO == null ? "" : _outputBO.Name );
		}

		#endregion

		#region Event Handlers
		private void btnOK_Click(object sender, EventArgs e)
		{
			CancelEventArgs cargs = new CancelEventArgs(false);
			OnValidateNewItem(cargs);
			if ( !cargs.Cancel ) {
				if ( this.AllowContinue )
					OnSaveContinue(new CancelEventArgs(false));
				else
					OnSaveClose(new CancelEventArgs(false));
			}
		}
		private void txtName_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
		{
			if ( e.KeyCode == Keys.Enter ) {
				CancelEventArgs cargs = new CancelEventArgs(false);
				OnValidateNewItem(cargs);
				if ( !cargs.Cancel ) {
					if ( this.AllowContinue )
						OnSaveContinue(new CancelEventArgs(false));
					else
						OnSaveClose(new CancelEventArgs(false));
				}
			}
		}

		private void btnSelInput_Click(object sender, EventArgs e)
		{
			Forms.FormSelectRepositoryEx frm = new OpenComposite.EII.Forms.FormSelectRepositoryEx(this.RepositoryItem);
			frm.AllowedTypes = new ItemType[] { ItemType.BObject };
			frm.DomainID = Global.Status.CurrentDomainId;
			frm.RepositoryID = Global.Status.CurrentRepositoryId;
			frm.MultiSelect = false;
			frm.SelectedItem = _inputBO;
			if ( frm.ShowDialog() == DialogResult.OK ) {
				_inputBO = frm.SelectedItem as BusinessObject;
				showInputOutput();
			}
		}
		private void btnViewInput_Click(object sender, EventArgs e)
		{
			showSchema(_inputBO);
		}
		private void btnRemoveInput_Click(object sender, EventArgs e)
		{
			_inputBO = null;
			showInputOutput();
		}

		private void btnSelOutput_Click(object sender, EventArgs e)
		{
			Forms.FormSelectRepositoryEx frm = new OpenComposite.EII.Forms.FormSelectRepositoryEx(this.RepositoryItem);
			frm.AllowedTypes = new ItemType[] { ItemType.BObject };
			frm.DomainID = Global.Status.CurrentDomainId;
			frm.RepositoryID = Global.Status.CurrentRepositoryId;
			frm.MultiSelect = false;
			frm.SelectedItem = _outputBO;
			if ( frm.ShowDialog() == DialogResult.OK ) {
				_outputBO = frm.SelectedItem as BusinessObject;
				showInputOutput();
			}
		}
		private void btnViewOutput_Click(object sender, EventArgs e)
		{
			showSchema(_outputBO);
		}
		private void btnRemoveOutput_Click(object sender, EventArgs e)
		{
			_outputBO = null;
			showInputOutput();
		}

		private void txtName_KeyPress(object sender, KeyPressEventArgs e)
		{
			e.Handled = !Helpers.IsValidCharForName(e.KeyChar);
		}

		private void txtDesc_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
		{
			if ( e.Control && e.KeyCode == Keys.Enter ) {
				CancelEventArgs cargs = new CancelEventArgs(false);
				OnValidateNewItem(cargs);
				if ( !cargs.Cancel ) {
					if ( this.AllowContinue )
						OnSaveContinue(new CancelEventArgs(false));
					else
						OnSaveClose(new CancelEventArgs(false));
				}
			}
		}

		#endregion


	}
}